package com.baic.ibase.controller;


import com.baic.ibase.models.req.common.ApiResponse;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.Objects;

@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(MethodArgumentNotValidException.class)
    public ApiResponse<String> handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
        return new ApiResponse<>(400, "参数校验失败", Objects.requireNonNull(e.getBindingResult().getFieldError()).getDefaultMessage(), null, false, null);
    }

    @ExceptionHandler(BusinessException.class)
    public ApiResponse<String> handleException(Exception e) {
        e.printStackTrace();
        return new ApiResponse<>(500, e.getMessage(), "业务错误", null, false, null);
    }

}
